package tool;

import java.util.Random;

public class RepeatRandom extends Random{
	//Manages different types of random number calls and assigns them to different generators so they can be reproduced.

	public static void main(String args[]){
		long sd = System.currentTimeMillis();
		System.out.println("Seed: "+sd);
		RepeatRandom generator = new RepeatRandom(sd,0,0);
		for(int i=0;i<5;i++){System.out.println(generator.nextDouble());}
		for(int i=0;i<5;i++){System.out.println(generator.nextInt());}

		System.out.println("--- Save and Restore ---");
		int iC = generator.iCalls; int dC = generator.dCalls;
		System.out.println("iCalls: "+iC);
		System.out.println("dCalls: "+dC);
		System.out.println("-------");
		for(int i=0;i<5;i++){System.out.println(generator.nextDouble());}
		for(int i=0;i<5;i++){System.out.println(generator.nextInt());}

		System.out.println("-------");
		generator = new RepeatRandom(sd,5,5);
		for(int i=0;i<5;i++){System.out.println(generator.nextDouble());}
		for(int i=0;i<5;i++){System.out.println(generator.nextInt());}

		iC = generator.iCalls; dC = generator.dCalls;
		System.out.println("iCalls: "+iC);
		System.out.println("dCalls: "+dC);
		System.out.println("-------");
	}
	private Random iGen;

	private Random dGen;
	private int iCalls;

	private int dCalls;

	public RepeatRandom(long seed, int numIntCalls, int numDoubleCalls){
		iGen = new Random(seed); dGen = new Random(seed);

		iCalls = numIntCalls;
		dCalls = numDoubleCalls;

		for(int i=0;i<iCalls;i++){
			iGen.nextInt();
		}

		for(int j=0;j<dCalls;j++){
			dGen.nextDouble();
		}
	}

	public int getdCalls() {
		return dCalls;
	}

	public int getiCalls() {
		return iCalls;
	}

	public String getStatus(){
		return "i"+iCalls+" d"+dCalls;
	}

	@Override
	public double nextDouble(){
		dCalls = dCalls+1;
		return dGen.nextDouble();
	}

	@Override
	public int nextInt(){
		iCalls = iCalls+1;
		return iGen.nextInt();
	}

	@Override
	public int nextInt(int c){
		dCalls = dCalls+1;
		return (int)Math.round((dGen.nextDouble()*(c-1)));
	}

	public void setdCalls(int dCalls) {
		this.dCalls = dCalls;
	}

	public void setiCalls(int iCalls) {
		this.iCalls = iCalls;
	}

}
